package net.gz01.wallet.web.controller.merch;

import java.io.File;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;

import net.gz01.util.ZOExcel;
import net.gz01.util.ZOMD5;
import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.config.SystemEnum.FilePath;
import net.gz01.wallet.web.config.SystemEnum.PayOrderItemStatus;
import net.gz01.wallet.web.config.SystemEnum.PayOrderStatus;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.Merch;
import net.gz01.wallet.web.model.MerchUser;
import net.gz01.wallet.web.model.PayOrder;
import net.gz01.wallet.web.model.PayOrderItem;
import net.gz01.wallet.web.service.PayOrderService;

/**
 * 商户发起代付制单
 * @author zhongzhanfeng
 *
 */
public class PayOrderController extends BaseController{

	private PayOrderService payService = Duang.duang(PayOrderService.class);

	public void search() {
		render("trade/merchPayList.html");
	}

	public void listJson() {
		MerchUser merchUser = (MerchUser) getSession().getAttribute("loginMerchUser");
		ZOSearchCondition sc = super.getSearchCondition();
		sc.put("merchCode", merchUser.getMerchCode());
		renderJson(new PageRespon(PayOrder.dao.search(sc)));
	}

	public void searchItem() {
		String batchNo = getPara("batchNo");
		List<PayOrderItem> items = PayOrderItem.dao.selectByBatchNo(batchNo);
		PayOrder order = PayOrder.dao.selectByBatchNo(batchNo);
		setAttr("order", order);
		setAttr("items", items);
		render("trade/merchPayItemList.html");
	}

	public void addPage() {
		MerchUser merchUser = (MerchUser) getSession().getAttribute("loginMerchUser");
		Merch merch = Merch.dao.selectByMerchCode(merchUser.getMerchCode());
		setAttr("merch", merch);
		render("trade/merchPayAdd.html");
	}

	public void addPage1() {
		MerchUser merchUser = (MerchUser) getSession().getAttribute("loginMerchUser");
		Merch merch = Merch.dao.selectByMerchCode(merchUser.getMerchCode());
		setAttr("merch", merch);
		render("trade/add1.html");
	}

	public void addPage2() {
		MerchUser merchUser = (MerchUser) getSession().getAttribute("loginMerchUser");
		Merch merch = Merch.dao.selectByMerchCode(merchUser.getMerchCode());
		setAttr("merch", merch);
		render("trade/add2.html");
	}

	@Before(Tx.class)
	public void batchPay() throws Exception{
		MerchUser merchUser = (MerchUser) getSession().getAttribute("loginMerchUser");
		UploadFile upload = getFile();
		String payPassword = getPara("payPassword");
		if(ZOMD5.codeFor(payPassword).equalsIgnoreCase(merchUser.getPayPassword()) == false) {
			upload.getFile().delete();
			renderFail("支付密码不正确!");
		}else {
			List<String[]> data = ZOExcel.readXlsx(upload.getFile());
			List<String> errorMsg  = payService.batchPay(data,merchUser.getMerchCode());
			upload.getFile().delete();
			if(errorMsg.isEmpty()){
				renderSuccess();
			}else{
				StringBuffer sb = new StringBuffer();
				for (String msg : errorMsg) {
					sb.append(msg + "</br>");
				}
				renderFail(sb.toString());
			}
		}


	}

	public void reviewPage() {
		setAttr("batchNo", getPara("batchNo"));
		render("trade/merchPayReview.html");
	}

	@Before(Tx.class)
	public void review() {
		String payPassword = getPara("payPassword");
		String batchNo = getPara("batchNo");
		String reviewStatus = getPara("reviewStatus");
		MerchUser merchUser = (MerchUser) getSession().getAttribute("loginMerchUser");
		PayOrder order = PayOrder.dao.selectByBatchNo(batchNo);
		// 状态验证
		if (!PayOrderItemStatus.WaitOne.getValue().equals(order.getOrderStatus())) {
			renderFail("单据状态不能一审");
			return;
		}
		if(ZOMD5.codeFor(payPassword).equalsIgnoreCase(merchUser.getPayPassword())) {
			if("pass".equals(reviewStatus)) {
				PayOrderItem.dao.batchUpdate(batchNo, PayOrderItemStatus.OnePass.getValue());
				order.setOrderStatus(PayOrderStatus.OnePass.getValue());
				order.update();
			}else {
				PayOrderItem.dao.batchUpdate(batchNo, PayOrderItemStatus.OneFail.getValue());
				order.setOrderStatus(PayOrderStatus.OneFail.getValue());
				order.update();
			}
			renderSuccess();
		}else {
			renderFail("支付密码不正确");
		}
	}

	public void download(){
		renderFile(new File(FilePath.ReportTemplate.getValue() + "商户代付模版.xlsx"));
	}

	public void downloadReference(){
		renderFile(new File(FilePath.ReportTemplate.getValue() + "省份、市、开户行参照填写.xlsx"));
	}

}
